package com.cqie.hospitalescort.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqie.hospitalescort.DTO.HospitalDTO;
import com.cqie.hospitalescort.VO.HospitalInfoVO;
import com.cqie.hospitalescort.entity.EscortInfo;
import com.cqie.hospitalescort.entity.HospitalInfo;
import com.cqie.hospitalescort.enums.LevelEnum;
import com.cqie.hospitalescort.exception.BusinessException;
import com.cqie.hospitalescort.mapper.EscortInfoMapper;
import com.cqie.hospitalescort.mapper.HospitalInfoMapper;
import com.cqie.hospitalescort.service.HospitalInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.hospitalescort.service.file.AliYunService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gdy
 * @since 2025-02-24
 */
@Service
public class HospitalInfoServiceImpl extends ServiceImpl<HospitalInfoMapper, HospitalInfo> implements HospitalInfoService {

    @Autowired
    private HospitalInfoMapper hospitalInfoMapper;
    @Autowired
    private AliYunService aliYunService;
    @Autowired
    private EscortInfoMapper escortInfoMapper;

    /**
     * 医院列表
     *
     * @return
     */
    @Override
    public List<HospitalInfo> getList() {
        return hospitalInfoMapper.selectList(null);
    }

    /**
     * 查询详情
     *
     * @param hospitalId
     * @return
     */
    @Override
    public HospitalInfoVO getHospitalDetail(Long hospitalId) {
        HospitalInfo hospitalInfo = hospitalInfoMapper.selectById(hospitalId);

        HospitalInfoVO hospitalInfoVO = new HospitalInfoVO();
        BeanUtils.copyProperties(hospitalInfo, hospitalInfoVO);

        List<EscortInfo> escortInfos = escortInfoMapper.selectList(new LambdaQueryWrapper<EscortInfo>().eq(EscortInfo::getHospitalId, hospitalId));

        if (!CollectionUtils.isEmpty(escortInfos)) {
            hospitalInfoVO.setEscortInfoList(escortInfos.stream().map(EscortInfo::buildEscortDTO).collect(Collectors.toList()));
        }

        return hospitalInfoVO;
    }

    /**
     * 新增、修改医院
     *
     * @param dto
     * @param files
     */
    @Override
    public void saveHospital(HospitalDTO dto, List<MultipartFile> files) {
        HospitalInfo hospitalInfo = new HospitalInfo();

        if (Objects.nonNull(dto.getId())) {
            hospitalInfo = hospitalInfoMapper.selectById(dto.getId());
            hospitalInfo.setHospitalName(dto.getHospitalName());
            hospitalInfo.setHospitalDesc(dto.getHospitalDesc());
            hospitalInfo.setHospitalAddress(dto.getHospitalAddress());
            hospitalInfo.setSubjectHome(dto.getSubjectHome());
            hospitalInfo.setLevel(LevelEnum.getDesc(dto.getLevel()));
            if (Objects.nonNull(files)) {
                aliYunService.deleteFilesByUrl(Arrays.asList(hospitalInfo.getPictureUrl().split(",")));
            }
            try {
                List<String> list = aliYunService.uploadFiles(files);
                hospitalInfo.setPictureUrl(String.join(",", list));
            } catch (IOException e) {
                throw new BusinessException("图片上传失败");
            }
            hospitalInfoMapper.updateById(hospitalInfo);
        } else {
            BeanUtils.copyProperties(dto, hospitalInfo);
            try {
                List<String> list = aliYunService.uploadFiles(files);
                hospitalInfo.setPictureUrl(String.join(",", list));
            } catch (IOException e) {
                throw new BusinessException("图片上传失败");
            }
            hospitalInfoMapper.insert(hospitalInfo);
        }
    }

    /**
     * 删除医院
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        HospitalInfo hospitalInfo = hospitalInfoMapper.selectById(id);
        if (Objects.isNull(hospitalInfo)) {
            throw new BusinessException("医院不存在！");
        }
        hospitalInfoMapper.deleteById(id);
    }
}
